﻿using Styx;
using Styx.WoWInternals;
using Styx.WoWInternals.WoWObjects;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Paster.Managers
{
    internal static class Groups
    {
        public static bool MeInParty
        {
            get
            {
                var t = new List<WoWPartyMember>();
                t = StyxWoW.Me.GroupInfo.RaidMembers.Union(StyxWoW.Me.GroupInfo.PartyMembers).Where(p => p.ToPlayer() != null
                    && p.ToPlayer().Location.Distance(StyxWoW.Me.Location) <= 100).Distinct().ToList();
                return t.Count() > 0 ? true : false;
            }
        }

        public static IEnumerable<WoWPartyMember> WoWPartyMembers
        {
            get
            {
                return StyxWoW.Me.GroupInfo.RaidMembers.Union(StyxWoW.Me.GroupInfo.PartyMembers).Distinct().ToList();
            }
        }

        #region partycount
        public static int partyCount
        {
            get
            {
                return StyxWoW.Me.GroupInfo.NumRaidMembers == 0 ? StyxWoW.Me.GroupInfo.NumPartyMembers : StyxWoW.Me.GroupInfo.NumRaidMembers;
            }
        }
        #endregion

        public static List<WoWPlayer> Tanks
        {
            get
            {
                List<WoWPlayer> list = new List<WoWPlayer>();
                list = WoWPartyMembers.Where(p => p.HasRole(WoWPartyMember.GroupRole.Tank)).Select(p => p.ToPlayer()).ToList();
                return list;
            }
        }

        public static List<WoWPlayer> Healers
        {
            get
            {
                List<WoWPlayer> list = new List<WoWPlayer>();
                list = WoWPartyMembers.Where(p => p.HasRole(WoWPartyMember.GroupRole.Healer)).Select(p => p.ToPlayer()).ToList();
                return list;
            }
        }

        public static List<WoWPlayer> Damage
        {
            get
            {
                List<WoWPlayer> list = new List<WoWPlayer>();
                list = WoWPartyMembers.Where(p => p.HasRole(WoWPartyMember.GroupRole.Damage)).Select(p => p.ToPlayer()).ToList();
                return list;
            }
        }

        public static IEnumerable<WoWUnit> PartyMembers 
        { 
            get 
            {
                if (StyxWoW.Me.MinimapZoneText == "Proving Grounds")
                {
                    return ObjectManager.GetObjectsOfTypeFast<WoWUnit>().Where(p => p != null
                        && p.CanSelect
                        && p.IsFriendly);
                }
                return SearchAreaPlayers();
            } 
        }

        public static IEnumerable<WoWPlayer> SearchAreaPlayers()
        {
            return ObjectManager.GetObjectsOfTypeFast<WoWPlayer>().Where(p => p != null && p.IsInMyPartyOrRaid);
        }

        public static IEnumerable<WoWUnit> SearchAreaUnits()
        {
            return ObjectManager.GetObjectsOfTypeFast<WoWUnit>();
        }

        public static bool IsValidObject(WoWObject wowObject)
        {
            return (wowObject != null) && wowObject.IsValid;
        }

        public static List<WoWUnit> getPartyMembers()
        {
            if (StyxWoW.Me.MinimapZoneText == "Proving Grounds") return PulsePartyMembersPG();
            return PulsePartyMembers();
        }

        public static List<WoWUnit> PulsePartyMembers()
        {
            var results = new List<WoWUnit>();
            results = ObjectManager.GetObjectsOfTypeFast<WoWPlayer>().Where(p => p != null 
                && p.IsInMyPartyOrRaid).Select(p => p.ToUnit()).ToList();
            return results;
        }

        public static List<WoWUnit> PulsePartyMembersPG()
        {
            var results = new List<WoWUnit>();
            results = ObjectManager.GetObjectsOfTypeFast<WoWUnit>().Where(p => p != null
                && p.CanSelect
                && p.IsFriendly).ToList();
            return results;
        }


    }
}
